package test.serverframe.armc.server.manager.service.exec.dag;

import java.util.ArrayList;
import java.util.List;

public class NodeInstance {
	
	public static void main(String[] args) {

		List<String> parentsa = new ArrayList<>();
		List<String> childrena = new ArrayList<>();
		childrena.add("B");
		childrena.add("C");
		NodeInfo nodeA = new NodeInfo("A", parentsa, childrena);
		
		List<String> parentsb = new ArrayList<>();
		List<String> childrenb = new ArrayList<>();
		parentsb.add("A");
		childrenb.add("D");
		childrenb.add("E");
		NodeInfo nodeB = new NodeInfo("B", parentsb, childrenb);
		
		
		List<String> parentsc= new ArrayList<>();
		List<String> childrenc = new ArrayList<>();
		parentsc.add("A");
		childrenc.add("E");
		NodeInfo nodeC = new NodeInfo("C", parentsc, childrenc);
		
		
		List<String> parentsd = new ArrayList<>();
		List<String> childrend = new ArrayList<>();
		parentsd.add("B");
		NodeInfo nodeD = new NodeInfo("D", parentsd, childrend);
		
		
		List<String> parentse = new ArrayList<>();
		List<String> childrene = new ArrayList<>();
		parentse.add("B");
		parentse.add("C");
		NodeInfo nodeE = new NodeInfo("E", parentse, childrene);
		
		Dag dag = new Dag();
		dag.setNodeCreator(NodeInstance::create);
		dag.setHandlerOnNodeActivated(NodeInstance::handler_onNodeActivated);
		
		ArrayList<NodeInfo> nodeInfos = new ArrayList<>();
		nodeInfos.add(nodeA);
		nodeInfos.add(nodeB);
		nodeInfos.add(nodeC);
		nodeInfos.add(nodeD);
		nodeInfos.add(nodeE);
		dag.createDag(nodeInfos);
		
		dag.start();
		
	}
	
	static DagNode create(String id) {
		DagNodeTest dnt = new DagNodeTest(id);
		return dnt;
	}

	static void handler_onNodeActivated(DagNode node) {
		new Thread(()->{
		    DagNode node1 = node;
		    System.out.println("execute" + node1.getId());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            node1.putToDeath();
        }).start();
	}
}


